#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1010;

int a[N][N],n,m,dp[N][N];

void solve(){
	n = in,m = in;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)
			a[i][j] = in;
	memset(dp,0,sizeof(dp));
	for(int i = 1;i <= n;i++)
		for(int j = m;j >= 1;j--)
			dp[i][j] = max(dp[i-1][j],max(dp[i][j+1],dp[i-1][j+1]+a[i][j]));
	printf("%d\n",dp[n][1]);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	int t = in;
	while(t--) solve();
	return 0;
}

